package com.byxy.luntan.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.byxy.luntan.entity.User;
import com.byxy.luntan.service.UserService;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/")//

@Slf4j 
public class UserController {
	
	//1.显示页面
	//2.定义业务类与方法
	//3.定义Dao接口
	//4.单元测试业务接口
	//5.控制层与业务类整合
	//6.UI
	
	
	@Autowired
	private UserService userService;
	//登录业务
	@GetMapping("toLoginPage.do")
	public String login() {
		
		return "login";
	}
	@PostMapping("userLogin.do")
	public String doLogin(String userName, String userPassword, String tipId, HttpSession session) {
		// User 使用VO类代替，防止密码
		User u = userService.getByNameAndPassword(userName, userPassword);
		log.info("userName = " +userName );
		log.info("userPassword = " +userPassword );
		log.info("user = " +u );
		if (u == null)
			return "redirect:toLoginPage.do";
		else {
			u.setUserPassword("");
			session.setAttribute("USER", u);
			return "redirect:toMainPage.do";
		}

	}
	
	//注册业务
	@GetMapping("toSignUpPage.do")
	public String reg() {
		
		return "signUp";
	}
	
	
	@PostMapping("userSignUp.do")
	public String  doreg(String user_name,String user_nick,String user_password,Integer  user_type) {
		log.info("user_name =" + user_name );
		log.info("user_nick =" + user_nick );
		log.info("user_password =" + user_password );
		log.info("user_type =" + user_type );
		
		userService.save(user_name,user_nick,user_password,user_type);
		
		return "redirect:toLoginPage.do";
	}
	
	
	@GetMapping("userSignOut.do")
	public String logout(HttpSession session) {
		session.removeAttribute("USER");
		return "redirect:toMainPage.do";
	}
	
	
	
	
	
	
}
